// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   Service.java

package sql;

import bo.Users;
import bo.Users_service;
import control.database.DataBase;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Service
{

    public Service()
    {
    }

    public String insert(Users_service service)
        throws SQLException
    {
        String message = "";
        ResultSet rs = null;
        DataBase db = new DataBase();
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formateDate = sf.format(date);
        System.out.println(formateDate);
        String createDate = formateDate;
        String effectdate = formateDate;
        String invaliddate = formateDate;
        String lostdate = formateDate;
        System.out.println(invaliddate + "+++++++++++++++++++++++++++++++");
        String sql = "insert into users_service(servid,userid,createdate,effectdate,invaliddate,lostdate,state)values(" + service.getServid() + "," + service.getUserid() + ",'" + createDate + "','" + effectdate + "','" + effectdate + "','" + lostdate + "'," + service.getState() + ")";
        System.out.println("insert" + sql);
        db.executeUpdate(sql);
        db.closeStmt();
        db.closeConn();
        sql = "select * from users_service where servid=" + service.getServid();
        System.out.println("select insert" + sql);
        rs = db.executeQuery(sql);
        if(rs.next())
            message = "insert successfull";
        else
            message = "insert failse";
        rs.close();
        db.closeStmt();
        db.closeConn();
        return message;
    }

    public String update(Users_service us)
        throws SQLException
    {
        String message = "";
        ResultSet rs = null;
        DataBase db = new DataBase();
        String sql = "update users_service set servid=" + us.getServid() + " where userid=" + us.getUserid();
        db.executeUpdate(sql);
        db.closeStmt();
        db.closeConn();
        sql = "select * from users_service where userid=" + us.getUserid() + " and servid=" + us.getServid();
        rs = db.executeQuery(sql);
        for(message = "insert failse"; rs.next(); message = "insert successfull");
        rs.close();
        db.closeStmt();
        db.closeConn();
        return message;
    }

    public String update_state(Users_service us)
        throws SQLException
    {
        String message = "";
        ResultSet rs = null;
        DataBase db = new DataBase();
        String sql = "update users_service set state=" + us.getState() + " where userid=" + us.getUserid();
        db.executeUpdate(sql);
        db.closeStmt();
        db.closeConn();
        sql = "select * from users_service where userid=" + us.getUserid() + " and state=" + us.getState();
        rs = db.executeQuery(sql);
        for(message = "insert failse"; rs.next(); message = "insert successfull");
        rs.close();
        db.closeStmt();
        db.closeConn();
        return message;
    }

    public String delete(int userid)
        throws SQLException
    {
        String message = "";
        ResultSet rs = null;
        Users users = new Users();
        DataBase db = new DataBase();
        String sql = "delete from users_service where userid=" + userid;
        db.executeUpdate(sql);
        db.closeStmt();
        db.closeConn();
        sql = "select * from users_service where userid=" + userid;
        rs = db.executeQuery(sql);
        if(rs.next())
            message = "delete failse";
        else
            message = "delete successfull";
        rs.close();
        db.closeStmt();
        db.closeConn();
        return message;
    }

    public Users_service select(int userid)
    {
        Users_service user = new Users_service();
        ResultSet rs = null;
        Users users = new Users();
        DataBase db = new DataBase();
        String sql = "select * from users_service where userid=" + userid;
        System.out.println("sql=" + sql);
        rs = db.executeQuery(sql);
        try
        {
            while(rs.next()) 
            {
                user.setUserid(userid);
                user.setCreatedate(rs.getString("createdate"));
                user.setEffectdate(rs.getString("effectdate"));
                user.setInvaliddate(rs.getString("invaliddate"));
                user.setLostdate(rs.getString("lostdate"));
                user.setServid(rs.getShort("servid"));
                user.setState(rs.getShort("servid"));
            }
        }
        catch(Exception e)
        {
            System.out.println("errors=" + e.getMessage());
        }
        db.closeStmt();
        db.closeConn();
        return user;
    }

    public static void main(String args[])
    {
        String s = "";
        Users_service us = new Users_service();
        us.getUserid();
        us.setUserid(205);
        Service ser = new Service();
        try
        {
            s = ser.update(us);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        System.out.println(s);
    }
}
